package sunday.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import sunday.common.kit.EncryptKit;
import sunday.common.kit.ShiroKit;
import sunday.common.kit.StringKit;
import sunday.pojo.manager.Manager;
import sunday.pojo.manager.ResultJson;
import sunday.pojo.school.*;
import sunday.pojo.teacher.Teacher;
import sunday.service.manager.ManagerService;

import java.io.IOException;
import java.util.List;

/**
 * Created by 96216 on 2017/8/7.
 */
@Controller
@RequestMapping("/m")
public class ManagerController {
    @Autowired
    private ManagerService managerService;


    /**
     * 获取当前管理员身份id
     * @return
     */
    private Integer getCurrentManagerId(){
        return ((Manager) ShiroKit.getSession().getAttribute("currentManager")).getManagerId();
    }


    @RequestMapping("/main")
    public String main(){
        return  "/manager/main/main";
    }


    /**
     * 按条件查询专业
     * 非ajax方式
     * @param speName
     * @param pn
     * @param model
     * @return
     */
    @RequestMapping("/getSpecialty")
    public String getSpecialties(@RequestParam(value = "speName",required = false) String speName, @RequestParam(value = "pn",defaultValue = "1") int pn, Model model){

        PageHelper.startPage(pn,10);
        SpeQuestion question=new SpeQuestion(speName);
        List<SpeWithStu> list=managerService.selectSpecialty(question);
        if(list!=null && list.size()>0){
            PageInfo page=new PageInfo(list,5);
            // modelAndView.addObject("page",page);
            model.addAttribute("page",page);
            return "/manager/display/specialty";
        }
        else {
            model.addAttribute("exception", "查询出错，没有找到该专业班级信息");
            return  "/common/error/error";
        }

    }

    /**
     * ajax方式获取专业信息
     * @param speName
     * @param pn
     * @return
     */
    @RequestMapping(value = "/getSpecialties",method = RequestMethod.GET)
    @ResponseBody
    public ResultJson getSpecialties(@RequestParam(value = "speName",required = false) String speName, @RequestParam(value = "pn",defaultValue = "1") int pn) {
        PageHelper.startPage(pn,10);
        SpeQuestion question=new SpeQuestion(StringKit.trim(speName));
        List<SpeWithStu> list=managerService.selectSpecialty(question);
        if(list!=null && list.size()>0){
            PageInfo page=new PageInfo(list,5);
            return ResultJson.success().add("page",page);
        }else{
            return ResultJson.success().add("page",null);
        }

    }

    @RequestMapping("/addSpe")
    public String  addSpe(){
        return "/manager/main/add_specialty";
    }

    @RequestMapping("/saveSpe")
    public String saveSpe(@RequestParam(value = "specialtyId") Integer specialtyId,@RequestParam("name") String name,Model model){

        if(specialtyId==null||name==null||"".equals(name)){
            model.addAttribute("exception", "请完善信息在进行操作");
            return  "/common/error/error";
        }
        Specialty specialty=new Specialty(null,specialtyId,name);
        managerService.addSpecialty(specialty);
        model.addAttribute("exception", "添加专业信息成功");
        return  "/common/error/error";
    }

    @RequestMapping("/delSpecialty")
    public String delSpecialty(@RequestParam(value = "specialtyId")Integer specialtyId,Model model){
        Specialty specialty=new Specialty(null,specialtyId,null);
        if(managerService.delSpecialty(specialty)){
            model.addAttribute("exception", "删除成功");
            return  "/common/error/error";
        }
        model.addAttribute("exception", "删除失败");
        return  "/common/error/error";

    }

//    @RequestMapping(value = "/saveSpecialties", method = RequestMethod.POST)
//    @RequiresAuthentication
//    @RequiresPermissions(value = "shiro:sys:manager")
//    @ResponseBody
//    public Map<String,Object> saveSpecialties(@RequestParam(value = "file") List<MultipartFile> file)throws IOException{
//        int i=0;
//        Map<String, Object> info = new HashMap<>();
//        if (null != file && file.size() > 0) {
//            for (MultipartFile f : file) {
//                InputStream in=f.getInputStream();
//                i+=managerService.readExcel(in);
//
//            }
//            info.put("isSuccess", true);
//        } else {
//            info.put("isSuccess", false);
//        }
//        return info;
//    }




    @RequestMapping("/updateStu")
    public String updateStu(@RequestParam(value = "studentId") Integer studentId,Model model){
        Student stu=new Student(studentId,null,null,null,null,null,null);
        List<Student> list= managerService.selectStudent(stu);
        Student student=list.get(0);
        model.addAttribute("student",student);
        return "/manager/main/update_student";
    }


    @RequestMapping("/updateStudent")
    public String updateStu(@RequestParam(value = "studentId") Integer studentId,@RequestParam(value = "name",required = false) String name,
                            @RequestParam(value = "name")String password,@RequestParam(value = "specialtyId") Integer specialtyId,
                            @RequestParam(value = "gender",required = false)String gender,
                            @RequestParam(value = "cellphone",required = false)String cellphone,@RequestParam(value = "email",required = false)String email,Model model){
        Student student=new Student(studentId,password,name,specialtyId,gender,cellphone,email);
        managerService.updateStudent(student);
        model.addAttribute("exception", "更新学生信息成功");
        return  "/common/error/error";
    }

    @RequestMapping("/addStu")
    public String addStu(){
        return "/manager/main/add_student";
    }

    @RequestMapping("/saveStu")
    public String saveStu(@RequestParam(value = "studentId") Integer studentId, @RequestParam(value = "name",required = false) String name,
                          @RequestParam(value = "name")String password,@RequestParam(value = "specialtyId") Integer specialtyId,
                          @RequestParam(value = "gender",required = false)String gender,
                          @RequestParam(value = "cellphone",required = false)String cellphone,@RequestParam(value = "email",required = false)String email,Model model){
        String pass=EncryptKit.md5(StringKit.trim(password));
        Student student=new Student(studentId,pass,name,specialtyId,gender,cellphone,email);
        managerService.saveStudent(student);
        model.addAttribute("exception", "添加学生信息成功");
        return  "/common/error/error";
    }

    @RequestMapping("/delStu")
    public String delStu(@RequestParam(value = "studentId") Integer studentId, Model model){
        Student student=new Student(studentId,null,null,null,null,null,null);
        managerService.delStu(student);
        model.addAttribute("exception", "删除学生信息成功");
        return  "/common/error/error";
    }

    @RequestMapping("/toStudent")
    public String getStudents(@RequestParam(value = "specialtyId",required = false) Integer specialtyId,@RequestParam(value = "studentId",required = false) Integer studentId, @RequestParam(value = "pn",defaultValue = "1") int pn, Model model){

        PageHelper.startPage(pn,10);
        Student student=new Student(studentId,null,null,specialtyId,null,null,null);
        List<Student> list=managerService.selectStudent(student);
        if(list!=null && list.size()>0){
            PageInfo page=new PageInfo(list,5);
            // modelAndView.addObject("page",page);
            model.addAttribute("page",page);
            model.addAttribute("specialtyId",specialtyId);
            return "/manager/display/student";
        }
        else {
            model.addAttribute("exception", "查询出错，没有找到该生信息");
            return  "/common/error/error";
        }
    }



    @RequestMapping("/getCourse")
    public String getCourse(@RequestParam(value = "name",required = false) String name, @RequestParam(value = "pn",defaultValue = "1") int pn, Model model){
        Course course=new Course(null,name,null,null);
        PageHelper.startPage(pn,10);
        List<Course> list=managerService.selectCourse(course);
        if(list!=null && list.size()>0){
            PageInfo page=new PageInfo(list,5);
            // modelAndView.addObject("page",page);
            model.addAttribute("page",page);
            return "/manager/display/course";
        }
        else {
            model.addAttribute("exception", "查询出错，没有找到课程信息");
            return  "/common/error/error";
        }
    }

    @RequestMapping("/addCou")
    public String addCou(){
        return "/manager/main/add_course";
    }

    @RequestMapping("/saveCourse")
    public  String saveCourse(@RequestParam(value = "courseId") Integer courseId, @RequestParam(value = "name") String name,
                              @RequestParam(value = "period",required = false)Integer period,@RequestParam(value = "credit",required = false) Integer credit,Model model){

        Course course=new Course(courseId,name,period,credit);
        managerService.saveCourse(course);
        model.addAttribute("exception", "添加课程信息成功");
        return  "/common/error/error";
    }


    @RequestMapping("/delCou")
    public String delCou(@RequestParam(value = "courseId") Integer courseId, Model model){
        Course course=new Course(courseId,null,null,null);
        managerService.delCou(course);
        model.addAttribute("exception", "删除课程信息成功");
        return  "/common/error/error";
    }

    @RequestMapping("/updateCou")
    public String updateCou(@RequestParam(value = "courseId") Integer courseId,Model model) {
        Course cou=new Course(courseId,null,null,null);
        List<Course> list = managerService.selectCourse(cou);
        Course course = list.get(0);
        model.addAttribute("course", course);
        return "/manager/main/update_course";
    }

    @RequestMapping("/updateCourse")
    public String updateCourse(@RequestParam(value = "courseId") Integer courseId,@RequestParam(value = "name") String name,@RequestParam(value = "period",required = false)Integer period,
                               @RequestParam(value = "credit",required = false) Integer credit,Model model){
        Course course=new Course(courseId,name,period,credit);
        managerService.updateCourse(course);
        model.addAttribute("exception", "更新课程信息成功");
        return  "/common/error/error";
    }


    @RequestMapping("/getTeacher")
    public String getTeacher(@RequestParam(value = "name",required = false) String name, @RequestParam(value = "pn",defaultValue = "1") int pn, Model model){
        Teacher teacher=new Teacher(null,null,name,null,null,null);
        PageHelper.startPage(pn,10);
        List<Teacher> list=managerService.selectTeacher(teacher);
        if(list!=null && list.size()>0){
            PageInfo page=new PageInfo(list,5);
            model.addAttribute("page",page);
            return "/manager/display/teacher";
        }
        else {
            model.addAttribute("exception", "查询出错，没有找到教师信息");
            return  "/common/error/error";
        }
    }


    @RequestMapping("/addTea")
    public String addTea(){
        return "/manager/main/add_teacher";
    }


    @RequestMapping("/saveTeacher")
    public String saveTeacher(@RequestParam(value = "teacherId") Integer teacherId, @RequestParam(value = "name",required = false) String name,
                          @RequestParam(value = "password")String password,@RequestParam(value = "gender",required = false)String gender,
                          @RequestParam(value = "position",required = false)String position,@RequestParam(value = "office",required = false)String office,Model model){
        String pass=EncryptKit.md5(StringKit.trim(password));
        Teacher teacher=new Teacher(teacherId,pass,name,gender,position,office);
        managerService.saveTeacher(teacher);
        model.addAttribute("exception", "添加教师信息成功");
        return  "/common/error/error";
    }

    @RequestMapping("/updateTea")
    public String updateTea(@RequestParam(value = "teacherId") Integer teacherId,Model model) {
        Teacher tea=new Teacher(teacherId,null,null,null,null,null);
        List<Teacher> list = managerService.selectTeacher(tea);
        Teacher teacher = list.get(0);
        model.addAttribute("teacher", teacher);
        return "/manager/main/update_teacher";
    }


    @RequestMapping("/updateTeacher")
    public String updateTeacher(@RequestParam(value = "teacherId") Integer teacherId, @RequestParam(value = "name",required = false) String name,
                              @RequestParam(value = "password")String password,@RequestParam(value = "gender",required = false)String gender,
                              @RequestParam(value = "position",required = false)String position,@RequestParam(value = "office",required = false)String office,Model model){
        Teacher teacher=new Teacher(teacherId,password,name,gender,position,office);
        managerService.updateTeacher(teacher);
        model.addAttribute("exception", "更新教师信息成功");
        return  "/common/error/error";
    }

    @RequestMapping("/delTea")
    public String delTea(@RequestParam(value = "teacherId")Integer teacherId,Model model){
        Teacher teacher=new Teacher(teacherId,null,null,null,null,null);
        managerService.delTeacher(teacher);
        model.addAttribute("exception", "删除教师信息成功");
        return  "/common/error/error";
    }
}
